#include "stdafx.h"
#include "UnitTest.h"

#ifndef LOG
#	define LOG printf
#endif

#ifndef MB_ASSERT
#	define MB_ASSERT(expr,...)	assert((expr))
#endif



namespace Melting
{
	namespace UnitTest
	{
		std::vector< TestFuncPtr > gaTests;  

		//===================================================================================
		void RegisterTest( TestFuncPtr pfuncTest )
		{
			std::vector< TestFuncPtr >:: iterator itFound = find( gaTests.begin(), gaTests.end(), pfuncTest ) ;
			
			// Only register the test if it's not already registered
			if( itFound == gaTests.end() )
			{
				gaTests.push_back( pfuncTest );				
			}
		}
		
		//===================================================================================
		void RemoveAllTests( void )
		{
			gaTests.clear();
			gaTests.resize(0);
		}

		//===================================================================================
		void ProcessAllTests( bool bAbortOnError )
		{ 
			LOG("\n-------------------------------------\n");
			LOG("-------------------------------------\n");
			LOG("-------- STARTING UNIT TESTS --------\n");
			LOG("-------------------------------------\n");

			for (unsigned int iTest = 0; iTest  < gaTests.size(); iTest ++ )
			{
				LOG( "# %d ", iTest );

				if ( bAbortOnError && ! gaTests[ (unsigned int)iTest  ] ()  )
				{
					MB_ASSERT( false,  "\n####### ERROR in the previous Unit Test #########\n" );
				}
			}		
			
			LOG("-------------------------------------\n");
			LOG("--------- END OF UNIT TESTS ---------\n");
			LOG("-------------------------------------\n");
			LOG("-------------------------------------\n");
		} 
	}
}